<template>
  <!-- 车主数据》车辆颜色 -->
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="4"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :defaultSelections="selectedRows"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
      :onSyncTableData="syncTableDataHandle"
      height="auto"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button v-if="dataType !== '10461001'" v-show="dealerType !== '25381002'" size="small" type="primary" :click="addHandle" :auth-list="auths" auth-mark="/api/basedata/vehicleProduct/colors">新增</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog
      :visible.sync="visible"
      :title="dialogTitle"
      destroyOnClose
      width="30%"
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <colorModal :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import _ from 'lodash';
import { getColors, getColorsOEM } from '@/api/common/cars/colorCars';
import { getDataType } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';
import colorModal from './colorModal';
import { authority } from '@/utils/authMixin';
import Cookies from 'js-cookie';
export default {
  name: 'Colorcars',
  mixins: [dictionary,authority],
  components: {
    colorModal
  },
  data() {
    return {
      topFilterList: this.createTopFilters(),
      fetchParams: {},
      visible: false,
      dialogTitle: '',
      fetchApi: Cookies.get('dataType') !== '10461001'?getColorsOEM:getColors,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf: { add: '新增', edit: '编辑', show: '显示' },
      currentPageKeys: [],
      selectedKeys: [],
      selectedRows: [],
      dealerType: Cookies.get('dealerType'),
      dataType: getDataType('dataType'),// 判断是否为厂端
    };
  },
  watch: {
    currentPageKeys(newVal, oldVal) {
      if (newVal.length > oldVal.length) {
        // 新增
        this.selectedKeys = [...new Set([...this.selectedKeys, ...newVal])];
      } else if (!this.state) {
        // 移除
        this.selectedKeys = _.difference(this.selectedKeys, _.difference(oldVal, newVal));
      }
      this.state = false;
    }
  },
  methods: {
    selectChangeHandle(rows) {
      this.currentPageKeys = rows.map(x => x.colorId);
    },
    syncTableDataHandle(list, isFirst) {
      if (isFirst) {
        this.state = true;
        this.selectedRows = list.filter(x => this.selectedKeys.includes(x.colorId));
      }
    },
    createTopFilters() {
      let dataType = Cookies.get('dataType');// 判断是否为厂端
      return [
        {
          type: 'SELECT',
          label: '颜色类型',
          fieldName: 'colorType',
          placeholder: '请选择',
          itemList: this.createDictList('1608')
        },
        {
          type: 'INPUT',
          label: '颜色代码',
          fieldName: 'colorCode',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '颜色名称',
          fieldName: 'colorName',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'dataSources',
          placeholder: '请选择',
          hidden: dataType === '10461001'?false:true,
          itemList: this.createDictList('1045')
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        }
      ];
    },
    createTableColumns() {
      let dealerType = Cookies.get('dealerType');
      let dataType = Cookies.get('dataType');// 判断是否为厂端
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button containerStyle={dealerType !== '25381002' ? {}:{display:'none'}} size="mini" type="text" disabled={dataType === '10461001' && (props.row.dataSources == 10451002 || props.row.dataSources == 10451003)} onClick={() => this.editHandle(props.row)} auth-list={this.auths} auth-mark={'/api/basedata/vehicleProduct/colors/${id}'}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '颜色类型',
          dataIndex: 'colorType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1608'),
          render: props => {
            return <span>{this.createDictText(props.row.colorType, '1608')}</span>;
          }
        },
        {
          title: '颜色代码',
          dataIndex: 'colorCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '颜色名称',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'dataSources',
          hidden: dataType === '10461001'?false:true,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1045'),
          render: props => {
            return <span>{this.createDictText(props.row.dataSources, '1045')}</span>;
          }
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isValid, '1004')}</span>;
          }
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.dialogTitle = `车辆颜色${this.conf[this.handleType]}`;
      this.visible = true;
    },
    changeHandle(val) {
      this.loadTableData(val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.loadTableData(this.fetchParams);
      }
    },
    loadTableData(val) {
      this.fetchParams = { ...val };
      this.selectedKeys = [];
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {}
};
</script>

<style lang="less" scoped>
</style>
